package com.dhwl.hsh.merchant.ui.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.dhwl.hsh.merchant.R;
import com.dhwl.hsh.merchant.presenter.MainPresenter;
import com.dhwl.hsh.merchant.ui.fragment.HomeFragment;
import com.dhwl.hsh.merchant.ui.fragment.MyOrderFragment;
import com.dhwl.hsh.merchant.ui.fragment.PersonalFragment;

/**
 * desc
 *
 * @author Derek.Kidd
 * @time 2016/4/8 17:53
 */
public class MainActivity extends BaseActivity {


    public static final String CURRENT_TAB_TAG = "current_tab_tag";


    private FragmentTabHost mFragmentTabHost;


    private Class<?> fragmentArray[] = {HomeFragment.class, MyOrderFragment.class, PersonalFragment.class};

    public static final int titleArray[] = {R.string.function_bar_home, R.string.function_bar_my_order,
            R.string.function_bar_personal};

    private int iconArray[] = {R.drawable.selector_functionbar_home, R.drawable.selector_functionbar_order,
            R.drawable.selector_functionbar_user};


    private String currentTab;


    MainPresenter mainPresenter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        currentTab = getString(titleArray[0]);
        mFragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mFragmentTabHost.setup(MainActivity.this, getSupportFragmentManager(), R.id.realtabcontent);

        for (int i = 0; i < fragmentArray.length; i++) {
            TabHost.TabSpec tabSpec =
                    mFragmentTabHost.newTabSpec(getResources().getString(titleArray[i])).setIndicator(getTabItemView(i));
            mFragmentTabHost.addTab(tabSpec, fragmentArray[i], null);
        }
        mFragmentTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                currentTab = tabId;
            }
        });
    }

    @Override
    protected void initData() {
        mainPresenter = new MainPresenter();
//        mainPresenter.getHtmlPage();
        mainPresenter.getArea();
    }

    /**
     * 设置tab显示
     *
     * @param tag
     * @see [类、类#方法、类#成员]
     */
    public void setCurrentTabByTag(String tag) {
        mFragmentTabHost.setCurrentTabByTag(tag);
    }

    @Override
    protected void onResume() {
        // 解决 java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
        // 即不能在"onSaveInstanceState"之后改变fragment问题
        // 参考http://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html
        if (TextUtils.isEmpty(currentTab)) {
            setCurrentTabByTag(getString(titleArray[0]));
        } else {
            setCurrentTabByTag(currentTab);
        }


        super.onResume();
    }

    private View getTabItemView(int index) {
        LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
        View view = layoutInflater.inflate(R.layout.home_buttom_item, null);

        ImageView imageView = (ImageView) view.findViewById(R.id.home_buttom_item_iv);
        imageView.setImageResource(iconArray[index]);

        TextView textView = (TextView) view.findViewById(R.id.home_buttom_item_tv);
        textView.setText(titleArray[index]);
        textView.setTextColor(getResources().getColor(R.color.home_buttom_tv_color));
        return view;
    }




    //    void retrofitTest() {
//        Call<List<Users>> repos = ApiManager.getInstance().getService().getUser("octocat");
//        // execute（同步）或者enqueue（异步）
//        repos.enqueue(new Callback<List<Users>>() {
//            @Override
//            public void onResponse(Call<List<Users>> call, Response<List<Users>> response) {
//
//                if (response.isSuccessful()) {
//                    List<Users> user = response.body();
//                } else {
//                    int statusCode = response.code();
//
//                    // handle request errors yourself
//
//                    Log.d("MainActivity", statusCode + ":" + errorBody.toString());
//                }
//                Toast.makeText(MainActivity.this, "onResponse", Toast.LENGTH_LONG).show();
//                Log.d("MainActivity", response.toString());
//
//            }
//
//            @Override
//            public void onFailure(Call<List<Users>> call, Throwable t) {
//                Toast.makeText(MainActivity.this, "onResponse", Toast.LENGTH_LONG).show();
//                Log.d("MainActivity", t.toString());
//            }
//        });
//    }

}
